package com.memo4android.widget;

import com.memo4android.MainView;
import com.memo4android.NewNoteActivity;
import com.memo4android.NewNoteFolderActivity;
import com.memo4android.NewTodoActivity;
import com.memo4android.NoteActivity;
import com.memo4android.R;
import com.memo4android.ToDoListsActivity;
import com.memo4android.model.AppModel;
import com.memo4android.model.Dossier;
import com.memo4android.model.Item;
import com.memo4android.model.ItemNote;
import com.memo4android.model.ItemTodoList;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider {
	
	public static String ACTION_WIDGET_ITEM1 = "ActionWidgetItem1";
	public static String ACTION_WIDGET_ITEM2 = "ActionWidgetItem2";
	public static String ACTION_WIDGET_ITEM3 = "ActionWidgetItem3";
	public static String ACTION_WIDGET_ITEM4 = "ActionWidgetItem4";
	public static String ACTION_WIDGET_ITEM5 = "ActionWidgetItem5";
	public static String ACTION_WIDGET_ITEM6 = "ActionWidgetItem6";
	public static String ACTION_WIDGET_UP = "ActionWidgetUp";
	public static String ACTION_WIDGET_DOWN = "ActionWidgetDown";
	public static String ACTION_WIDGET_REFRESH = "ActionWidgetRefresh";
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		AppModel model = AppModel.getInstance(context);		
		WidgetContext weedCtxt = WidgetContext.getInstance(model);
		
		RemoteViews views = new RemoteViews(context.getPackageName(),
				R.layout.frame_widget);
		
		System.out.println("______onReceive UP 3");
			
		//Creation Note
		Intent intent = new Intent(context, NewNoteActivity.class);
		intent.putExtra("folderName", "__HOME");
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
        views.setOnClickPendingIntent(R.id.note_widget, pendingIntent);
        
		//Creation Dossier
		Intent intentDossier = new Intent(context, NewNoteFolderActivity.class);
        PendingIntent pendingIntentDossier = PendingIntent.getActivity(context, 0, intentDossier, Intent.FLAG_ACTIVITY_NEW_TASK);
        views.setOnClickPendingIntent(R.id.dossier_widget, pendingIntentDossier);    
        
		//Creation TodoList
		Intent intentTodoList = new Intent(context, NewTodoActivity.class);
        PendingIntent pendingIntentTodoList = PendingIntent.getActivity(context, 0, intentTodoList, Intent.FLAG_ACTIVITY_NEW_TASK);
        views.setOnClickPendingIntent(R.id.todo_widget, pendingIntentTodoList);   
        
        //Refresh Widget
		Intent intentRefresh= new Intent(context, WidgetProvider.class);
		intentRefresh.setAction(ACTION_WIDGET_REFRESH);
        PendingIntent pendingIntentRefresh = PendingIntent.getBroadcast(context, 0, intentRefresh, 0);
        views.setOnClickPendingIntent(R.id.refresh_widget, pendingIntentRefresh);   
        
        //All Notes
		Intent intentAllNotes = new Intent(context, MainView.class);
        PendingIntent pendingIntentAllNotes = PendingIntent.getActivity(context, 0, intentAllNotes, Intent.FLAG_ACTIVITY_NEW_TASK);
        views.setOnClickPendingIntent(R.id.button_allnotes_widget, pendingIntentAllNotes);   
        
        //All Todos
		Intent intentAllTodos = new Intent(context, ToDoListsActivity.class);
        PendingIntent pendingIntentAllTodos = PendingIntent.getActivity(context, 0, intentAllTodos, Intent.FLAG_ACTIVITY_NEW_TASK);
        views.setOnClickPendingIntent(R.id.button_alltodos_widget, pendingIntentAllTodos);   
        
        //Actions items
		Intent intentItem = new Intent(context, WidgetProvider.class);
		intentItem.setAction(ACTION_WIDGET_ITEM1);
        PendingIntent pendingIntentItem = PendingIntent.getBroadcast(context, 0, intentItem, 0);
        views.setOnClickPendingIntent(R.id.tv1_widget, pendingIntentItem);   
        
        Intent intentItem2 = new Intent(context, WidgetProvider.class);
        intentItem2.setAction(ACTION_WIDGET_ITEM2);
        PendingIntent pendingIntentItem2 = PendingIntent.getBroadcast(context, 0, intentItem2, 0);
        views.setOnClickPendingIntent(R.id.tv2_widget, pendingIntentItem2);   
        
        Intent intentItem3 = new Intent(context, WidgetProvider.class);
        intentItem3.setAction(ACTION_WIDGET_ITEM3);
        PendingIntent pendingIntentItem3 = PendingIntent.getBroadcast(context, 0, intentItem3, 0);
        views.setOnClickPendingIntent(R.id.tv3_widget, pendingIntentItem3);    
        
        Intent intentItem4 = new Intent(context, WidgetProvider.class);
        intentItem4.setAction(ACTION_WIDGET_ITEM4);
        PendingIntent pendingIntentItem4 = PendingIntent.getBroadcast(context, 0, intentItem4, 0);
        views.setOnClickPendingIntent(R.id.tv4_widget, pendingIntentItem4);     
        
        Intent intentItem5 = new Intent(context, WidgetProvider.class);
        intentItem5.setAction(ACTION_WIDGET_ITEM5);
        PendingIntent pendingIntentItem5 = PendingIntent.getBroadcast(context, 0, intentItem5, 0);
        views.setOnClickPendingIntent(R.id.tv5_widget, pendingIntentItem5);    
        
        Intent intentItem6 = new Intent(context, WidgetProvider.class);
        intentItem6.setAction(ACTION_WIDGET_ITEM6);
        PendingIntent pendingIntentItem6 = PendingIntent.getBroadcast(context, 0, intentItem6, 0);
        views.setOnClickPendingIntent(R.id.tv6_widget, pendingIntentItem6);    

        //Bouton UP
        Intent intentUp= new Intent(context, WidgetProvider.class);
        intentUp.setAction(ACTION_WIDGET_UP);
        PendingIntent pendingIntentUp = PendingIntent.getBroadcast(context, 0, intentUp, 0);
        views.setOnClickPendingIntent(R.id.up_widget, pendingIntentUp);          
        
        //Bouton DOWN
        Intent intentDown= new Intent(context, WidgetProvider.class);
        intentDown.setAction(ACTION_WIDGET_DOWN);
        PendingIntent pendingIntentDown = PendingIntent.getBroadcast(context, 0, intentDown, 0);
        views.setOnClickPendingIntent(R.id.down_widget, pendingIntentDown);     
        
	    //Affichage items      
        Item item0 = weedCtxt.getItem(0);
        if(item0 != null){
        	if(item0 instanceof ItemNote){
        		ItemNote note = (ItemNote) item0;
        		views.setTextViewText(R.id.tv1_widget, note.getTitre() + " - " + note.getContent());
        		views.setInt(R.id.tv1_widget, "setBackgroundResource", R.drawable.postit);
        	}
        	else if(item0 instanceof Dossier){
        		Dossier dossier = (Dossier) item0;
        		views.setTextViewText(R.id.tv1_widget, dossier.getTitre());
        		views.setInt(R.id.tv1_widget, "setBackgroundResource", R.drawable.folder);
        	}
        	else if(item0 instanceof ItemTodoList){
        		ItemTodoList todoList = (ItemTodoList) item0;
        		views.setTextViewText(R.id.tv1_widget, todoList.getTitre());
        		if(todoList.getPriorite().equals(ItemTodoList.FAIBLE))
        				views.setInt(R.id.tv1_widget, "setBackgroundResource", R.drawable.dossier_vert);
        		else if(todoList.getPriorite().equals(ItemTodoList.MOYENNE))
    				views.setInt(R.id.tv1_widget, "setBackgroundResource", R.drawable.dossier_orange);
        		else if(todoList.getPriorite().equals(ItemTodoList.FORTE))
    				views.setInt(R.id.tv1_widget, "setBackgroundResource", R.drawable.dossier_rouge);        		
        	}        	
        }
        else
        {
        	views.setTextViewText(R.id.tv1_widget, "");
        	views.setInt(R.id.tv1_widget, "setBackgroundResource", 0);
        }
        
        Item item1 = weedCtxt.getItem(1);
        if(item1 != null){
        	if(item1 instanceof ItemNote){
        		ItemNote note = (ItemNote) item1;
        		views.setTextViewText(R.id.tv2_widget, note.getTitre() + " - " + note.getContent());
        		views.setInt(R.id.tv2_widget, "setBackgroundResource", R.drawable.postit);
        	}
        	else if(item1 instanceof Dossier){
        		Dossier dossier = (Dossier) item1;
        		views.setTextViewText(R.id.tv2_widget, dossier.getTitre());
        		views.setInt(R.id.tv2_widget, "setBackgroundResource", R.drawable.folder);
        	}
        	else if(item1 instanceof ItemTodoList){
        		ItemTodoList todoList = (ItemTodoList) item1;
        		views.setTextViewText(R.id.tv2_widget, todoList.getTitre());
        		if(todoList.getPriorite().equals(ItemTodoList.FAIBLE))
        				views.setInt(R.id.tv2_widget, "setBackgroundResource", R.drawable.dossier_vert);
        		else if(todoList.getPriorite().equals(ItemTodoList.MOYENNE))
    				views.setInt(R.id.tv2_widget, "setBackgroundResource", R.drawable.dossier_orange);
        		else if(todoList.getPriorite().equals(ItemTodoList.FORTE))
    				views.setInt(R.id.tv2_widget, "setBackgroundResource", R.drawable.dossier_rouge);        		
        	}         	
        }
        else
        {
        	views.setTextViewText(R.id.tv2_widget, "");
        	views.setInt(R.id.tv2_widget, "setBackgroundResource", 0);
        }        
        
        Item item2 = weedCtxt.getItem(2);
        if(item2 != null){
        	if(item2 instanceof ItemNote){
        		ItemNote note = (ItemNote) item2;
        		views.setTextViewText(R.id.tv3_widget, note.getTitre() + " - " + note.getContent());
        		views.setInt(R.id.tv3_widget, "setBackgroundResource", R.drawable.postit);
        	}
        	else if(item2 instanceof Dossier){
        		Dossier dossier = (Dossier) item2;
        		views.setTextViewText(R.id.tv3_widget, dossier.getTitre());
        		views.setInt(R.id.tv3_widget, "setBackgroundResource", R.drawable.folder);
        	}
        	else if(item2 instanceof ItemTodoList){
        		ItemTodoList todoList = (ItemTodoList) item2;
        		views.setTextViewText(R.id.tv3_widget, todoList.getTitre());
        		if(todoList.getPriorite().equals(ItemTodoList.FAIBLE))
        				views.setInt(R.id.tv3_widget, "setBackgroundResource", R.drawable.dossier_vert);
        		else if(todoList.getPriorite().equals(ItemTodoList.MOYENNE))
    				views.setInt(R.id.tv3_widget, "setBackgroundResource", R.drawable.dossier_orange);
        		else if(todoList.getPriorite().equals(ItemTodoList.FORTE))
    				views.setInt(R.id.tv3_widget, "setBackgroundResource", R.drawable.dossier_rouge);        		
        	}         	
        }
        else
        {
        	views.setTextViewText(R.id.tv3_widget, "");
        	views.setInt(R.id.tv3_widget, "setBackgroundResource", 0);
        }   
		
        Item item3 = weedCtxt.getItem(3);
        if(item3 != null){
        	if(item3 instanceof ItemNote){
        		ItemNote note = (ItemNote) item3;
        		views.setTextViewText(R.id.tv4_widget, note.getTitre() + " - " + note.getContent());
        		views.setInt(R.id.tv4_widget, "setBackgroundResource", R.drawable.postit);
        	}
        	else if(item3 instanceof Dossier){
        		Dossier dossier = (Dossier) item3;
        		views.setTextViewText(R.id.tv4_widget, dossier.getTitre());
        		views.setInt(R.id.tv4_widget, "setBackgroundResource", R.drawable.folder);
        	}
        	else if(item3 instanceof ItemTodoList){
        		ItemTodoList todoList = (ItemTodoList) item3;
        		views.setTextViewText(R.id.tv4_widget, todoList.getTitre());
        		if(todoList.getPriorite().equals(ItemTodoList.FAIBLE))
        				views.setInt(R.id.tv4_widget, "setBackgroundResource", R.drawable.dossier_vert);
        		else if(todoList.getPriorite().equals(ItemTodoList.MOYENNE))
    				views.setInt(R.id.tv4_widget, "setBackgroundResource", R.drawable.dossier_orange);
        		else if(todoList.getPriorite().equals(ItemTodoList.FORTE))
    				views.setInt(R.id.tv4_widget, "setBackgroundResource", R.drawable.dossier_rouge);        		
        	}         	
        }
        else
        {
        	views.setTextViewText(R.id.tv4_widget, "");
        	views.setInt(R.id.tv4_widget, "setBackgroundResource", 0);
        }   
        
        Item item4 = weedCtxt.getItem(4);
        if(item4 != null){
        	if(item4 instanceof ItemNote){
        		ItemNote note = (ItemNote) item4;
        		views.setTextViewText(R.id.tv5_widget, note.getTitre() + " - " + note.getContent());
        		views.setInt(R.id.tv5_widget, "setBackgroundResource", R.drawable.postit);
        	}
        	else if(item4 instanceof Dossier){
        		Dossier dossier = (Dossier) item4;
        		views.setTextViewText(R.id.tv5_widget, dossier.getTitre());
        		views.setInt(R.id.tv5_widget, "setBackgroundResource", R.drawable.folder);
        	}
        	else if(item4 instanceof ItemTodoList){
        		ItemTodoList todoList = (ItemTodoList) item4;
        		views.setTextViewText(R.id.tv5_widget, todoList.getTitre());
        		if(todoList.getPriorite().equals(ItemTodoList.FAIBLE))
        				views.setInt(R.id.tv5_widget, "setBackgroundResource", R.drawable.dossier_vert);
        		else if(todoList.getPriorite().equals(ItemTodoList.MOYENNE))
    				views.setInt(R.id.tv5_widget, "setBackgroundResource", R.drawable.dossier_orange);
        		else if(todoList.getPriorite().equals(ItemTodoList.FORTE))
    				views.setInt(R.id.tv5_widget, "setBackgroundResource", R.drawable.dossier_rouge);        		
        	}         	
        }
        else
        {
        	views.setTextViewText(R.id.tv5_widget, "");
        	views.setInt(R.id.tv5_widget, "setBackgroundResource", 0);
        }   
        
        Item item5 = weedCtxt.getItem(5);
        if(item5 != null){
        	if(item5 instanceof ItemNote){
        		ItemNote note = (ItemNote) item5;
        		views.setTextViewText(R.id.tv6_widget, note.getTitre() + " - " + note.getContent());
        		views.setInt(R.id.tv6_widget, "setBackgroundResource", R.drawable.postit);
        	}
        	else if(item5 instanceof Dossier){
        		Dossier dossier = (Dossier) item5;
        		views.setTextViewText(R.id.tv6_widget, dossier.getTitre());
        		views.setInt(R.id.tv6_widget, "setBackgroundResource", R.drawable.folder);
        	}
        	else if(item5 instanceof ItemTodoList){
        		ItemTodoList todoList = (ItemTodoList) item5;
        		views.setTextViewText(R.id.tv6_widget, todoList.getTitre());
        		if(todoList.getPriorite().equals(ItemTodoList.FAIBLE))
        				views.setInt(R.id.tv6_widget, "setBackgroundResource", R.drawable.dossier_vert);
        		else if(todoList.getPriorite().equals(ItemTodoList.MOYENNE))
    				views.setInt(R.id.tv6_widget, "setBackgroundResource", R.drawable.dossier_orange);
        		else if(todoList.getPriorite().equals(ItemTodoList.FORTE))
    				views.setInt(R.id.tv6_widget, "setBackgroundResource", R.drawable.dossier_rouge);        		
        	}         	
        }
        else
        {
        	views.setTextViewText(R.id.tv6_widget, "");
        	views.setInt(R.id.tv6_widget, "setBackgroundResource", 0);
        }   		
		
		//Update du widget
		for (int i : appWidgetIds) {
			int widgetId =i;			
			appWidgetManager.updateAppWidget(widgetId, views);
		}
	}
	
	public void onReceive(Context context, Intent intent) { 
		AppModel model = AppModel.getInstance(context);		
		WidgetContext weedCtxt = WidgetContext.getInstance(model);
		
		if (intent.getAction().equals(ACTION_WIDGET_REFRESH)) {
        	AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        	ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());
        	int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
        	onUpdate(context, appWidgetManager, appWidgetIds);
		} else if (intent.getAction().equals(ACTION_WIDGET_UP)) {
			weedCtxt.remplirItemsUp();

        	AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        	ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());
        	int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
        	onUpdate(context, appWidgetManager, appWidgetIds);
		} else if (intent.getAction().equals(ACTION_WIDGET_DOWN)) {
			weedCtxt.remplirItemsDown();
			
        	AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        	ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WidgetProvider.class.getName());
        	int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
        	onUpdate(context, appWidgetManager, appWidgetIds);		
		} else if (intent.getAction().equals(ACTION_WIDGET_ITEM1)) {
	    	Item item = weedCtxt.getItem(0);
	    	if(item instanceof ItemNote){
	    		ItemNote note = (ItemNote) item;
	    		Intent noteIntent = new Intent(context, NoteActivity.class);
	    		noteIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
	    		noteIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    		noteIntent.putExtra("folderName", note.getParent().getTitre());
	    		noteIntent.putExtra("noteName", note.getTitre());
	    		context.startActivity(noteIntent);
	    	}
	    } else if (intent.getAction().equals(ACTION_WIDGET_ITEM2)) {
	    	Item item = weedCtxt.getItem(1);
	    	if(item instanceof ItemNote){
	    		ItemNote note = (ItemNote) item;
	    		Intent noteIntent = new Intent(context, NoteActivity.class);
	    		noteIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
	    		noteIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    		noteIntent.putExtra("folderName", note.getParent().getTitre());
	    		noteIntent.putExtra("noteName", note.getTitre());
	    		context.startActivity(noteIntent);
	    	}
	    } else if (intent.getAction().equals(ACTION_WIDGET_ITEM3)) {
	    	Item item = weedCtxt.getItem(2);
	    	if(item instanceof ItemNote){
	    		ItemNote note = (ItemNote) item;
	    		Intent noteIntent = new Intent(context, NoteActivity.class);
	    		noteIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
	    		noteIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    		noteIntent.putExtra("folderName", note.getParent().getTitre());
	    		noteIntent.putExtra("noteName", note.getTitre());
	    		context.startActivity(noteIntent);
	    	}
	    } else if (intent.getAction().equals(ACTION_WIDGET_ITEM4)) {
	    	Item item = weedCtxt.getItem(3);
	    	if(item instanceof ItemNote){
	    		ItemNote note = (ItemNote) item;
	    		Intent noteIntent = new Intent(context, NoteActivity.class);
	    		noteIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
	    		noteIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    		noteIntent.putExtra("folderName", note.getParent().getTitre());
	    		noteIntent.putExtra("noteName", note.getTitre());
	    		context.startActivity(noteIntent);
	    	}
	    } else if (intent.getAction().equals(ACTION_WIDGET_ITEM5)) {
	    	Item item = weedCtxt.getItem(4);
	    	if(item instanceof ItemNote){
	    		ItemNote note = (ItemNote) item;
	    		Intent noteIntent = new Intent(context, NoteActivity.class);
	    		noteIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
	    		noteIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    		noteIntent.putExtra("folderName", note.getParent().getTitre());
	    		noteIntent.putExtra("noteName", note.getTitre());
	    		context.startActivity(noteIntent);
	    	}
	    } else if (intent.getAction().equals(ACTION_WIDGET_ITEM6)) {
	    	Item item = weedCtxt.getItem(5);
	    	if(item instanceof ItemNote){
	    		ItemNote note = (ItemNote) item;
	    		Intent noteIntent = new Intent(context, NoteActivity.class);
	    		noteIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
	    		noteIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
	    		noteIntent.putExtra("folderName", note.getParent().getTitre());
	    		noteIntent.putExtra("noteName", note.getTitre());
	    		context.startActivity(noteIntent);
	    	}
	    } else {
	        super.onReceive(context, intent);
	    }
	}
}
